package cn.tedu.eb_film.service.impl;


import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.CinemaHallMapper;
import cn.tedu.eb_film.pojo.dto.CinemaHallAddNewDTO;
import cn.tedu.eb_film.pojo.dto.CinemaHallUpdateDTO;
import cn.tedu.eb_film.pojo.entity.CinemaHall;
import cn.tedu.eb_film.pojo.vo.CinemaHallListVO;
import cn.tedu.eb_film.pojo.vo.CinemaHallStandardVO;
import cn.tedu.eb_film.service.CinemaHallService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
@Slf4j
public class CinemaHallServiceImpl implements CinemaHallService {
    @Autowired
    CinemaHallMapper cinemaHallMapper;

    @Override
    public void addNew(CinemaHallAddNewDTO cinemaHallAddNewDTO) {
        log.debug("开始处理[添加影厅]的业务");
        int rows = cinemaHallMapper.countByNameAndCinemaId(
                cinemaHallAddNewDTO.getName(),
                cinemaHallAddNewDTO.getCinemaId());
        if (rows>=1){
            String message ="该影院已存在"+cinemaHallAddNewDTO.getName()+"影厅,请更名!";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        CinemaHall cinemaHall = new CinemaHall();
        BeanUtils.copyProperties(cinemaHallAddNewDTO,cinemaHall);
        cinemaHall.setState(0);
        rows = cinemaHallMapper.insert(cinemaHall);
        if (rows != 1 ){
            String message = "服务器忙,请稍后重试!!";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

    }

    @Override
    public void deleteOnLogic(Long id) {
        log.debug("开始处理[删除影厅]的业务");
        CinemaHallStandardVO cinemaHallStandardVO
                = cinemaHallMapper.selectById(id);
        if (cinemaHallStandardVO == null ||
                cinemaHallStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "要删除的影厅不存在!请稍后重试!!";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        CinemaHall cinemaHall = new CinemaHall();
        cinemaHall.setId(id);
        cinemaHall.setState(StateCode.STATE_DELETE.getValue());
        cinemaHallMapper.updateById(cinemaHall);
    }


    public void setEnable(Long id) {
        updateEnableById(id,1);
    }
    public void setDisable(Long id) {
        updateEnableById(id,0);
    }

    @Override
    public CinemaHallStandardVO getStandardById(Long id) {
        CinemaHallStandardVO cinemaHallStandardVO
                = cinemaHallMapper.selectById(id);
        if (cinemaHallStandardVO == null ||
                cinemaHallStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "查询的影厅不存在!请稍后重试!!";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return cinemaHallStandardVO;
    }

    public void updateEnableById(Long id,Integer enable) {
        CinemaHallStandardVO queryResult = cinemaHallMapper.selectById(id);
        if (queryResult == null ||
                queryResult.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = ENABLE_TEXT[enable]+"的影厅数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (enable.equals(queryResult.getState())){
            String message = ENABLE_TEXT[enable]+"失败,修改的目标状态与目前一致";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        CinemaHall update = new CinemaHall();
        update.setId(queryResult.getId());
        update.setState(enable);
        int rows = cinemaHallMapper.updateById(update);
        if (rows != 1){
            String message = "修改失败,服务器忙,请稍后尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }


    public void updateById(CinemaHallUpdateDTO cinemaHallUpdateDTO) {
        CinemaHallStandardVO queryResult = cinemaHallMapper.selectById(cinemaHallUpdateDTO.getId());
        if (queryResult == null ||
                queryResult.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "修改失败!修改的影厅数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        CinemaHall update = new CinemaHall();
        BeanUtils.copyProperties(cinemaHallUpdateDTO,update);
        int rows = cinemaHallMapper.updateById(update);
        if (rows != 1){
            String message = "修改失败,服务器忙,请稍后尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public List<CinemaHallListVO> listByCinemaId(Long cinemaId) {
        List<CinemaHallListVO> list = cinemaHallMapper.listByCinemaId(cinemaId);
        list.removeIf(hall -> hall.getState().equals(StateCode.STATE_DELETE.getValue()));
        return list;
    }
}
